<?php
/**
 * 
 * Controlador de Script
 * @author natanael
 * 
 */
class Script extends Controller{
	
        public function __construct()
        {
		    parent::Controller();
		    $this->load->database();
		    $this->load->model(array('Mcategoria','Mscript','Mfrom','mdepartamento'));
        }
        
        public function index()
        {
           $this->load->view('main', array('content_page' => 'script_list','menutop' => 0));	
        }
        
        public function historico()
        {
           $this->load->view('main', array('content_page' => 'scriptHistorico_list','menutop' => 0));
        }
                
        public function data()
        {
             $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		     $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'idScript'; // Columna para el order by
		     $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		     $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		     $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		      
		    $records = $this->Mscript->loadScript($start, $limit, $sortname, $sortorder);
		    
		    $totalRecords = count($records);

		    $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		              $page = $totalPages;

		        $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false) 
		    {
			     foreach($records as $objetos){

			     	 $from = $this->Mfrom->LoadRegistroById($objetos->getFromScript());
			     	
			     	  if($objetos->getTipoScript() == 1)
			     	     $tipo = "SMS";
			     	  else   
			     	     $tipo = "Mail";
			     	     
			     	$departamento = $this->mdepartamento->LoadEstatusById($objetos->getDepartamentoScript()); 
			     	$categoria = $this->Mcategoria->LoadRegistroById($objetos->getCategoriaScript());   
			     	 
				     $arr = $json_output['rows'][] = array(
				          'idScript'              => $objetos->getIdScripst(),
				          'titleScript'           => $objetos->getTitleScript(),
				          'subjectScript'         => $objetos->getSubjectScript(),
				          'fromScript'            => $from->getTitle(),
				          'tipoScript'            => $tipo,
				          'departamenoScript'     => $departamento->getNombre(),
				          'categoriaScript'       => $categoria->getCategoria()
				      );
			      }
		      }

		 echo json_encode($json_output);    
		      
        }
        
        public function dataHistorico()
        {
        
     
        	
             $page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		     $sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'Id_Script'; // Columna para el order by
		     $sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'desc'; // Orden (asc|desc)
		     $limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
		     $start = $limit * $page - $limit;
		     if($start < 0) $start = 0;
  
		      header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		      header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		      header("Cache-Control: no-cache, must-revalidate" );
		      header("Pragma: no-cache" );
		      
		    $records = $this->Mscript->loadScriptHistorico($start, $limit, $sortname, $sortorder);
		    
		    $totalRecords = count($records);

		    $totalPages = ceil($totalRecords/$limit);
		     if ($page > $totalPages) 
		              $page = $totalPages;

		        $json_output = array(
		            'page'		=> $page,
		            'total'		=> $totalPages,
		            'records'	=> $totalRecords,
		            'rows'		=> array()
		        );

		    if($records != false) 
		    {
			     foreach($records as $objetos){

			     	 $from = $this->Mfrom->LoadRegistroById($objetos->getIdFrom());
			     	
			     	  if($objetos->getTipo() == 1)
			     	     $tipo = "SMS";
			     	  else   
			     	     $tipo = "Mail";
			     	     
			     	$departamento = $this->mdepartamento->LoadEstatusById($objetos->getIdDepartamento()); 
			     	$categoria = $this->Mcategoria->LoadRegistroById($objetos->getIdCategoria());   
			     	 
				     $arr = $json_output['rows'][] = array(
				          'idScript'              => $objetos->getIdScript(),
				          'titleScript'           => $objetos->getTitulo(),
				          'subjectScript'         => $objetos->getSubTitulo(),
				          'fromScript'            => $from->getTitle(),
				          'tipoScript'            => $tipo,
				          'departamenoScript'     => $departamento->getNombre(),
				          'categoriaScript'       => $categoria->getCategoria()
				      );
			      }
		      }

		 echo json_encode($json_output);    
		      
        }
        
        public function add()
	    {  
	       	
	       $this->load->view('main', array('content_page'   => 'script_add',
	                                       'menutop'        => 0,
	                                       'from'           => $this->Mfrom->loadFrom(),
	                                       'departamentos'  => $this->mdepartamento->LoadEstatus(),
	                                       'categoria'      => $this->Mcategoria->loadCategoriaFrom()));
	    }
	    
	    public function edit($id)
	    {
	    	 $registro = $this->Mcategoria->LoadRegistroById($id);
	         $registro = $this->Mscript->LoadRegistroById($id);
	    	 
	         $this->load->view('main', array('content_page'   => 'script_edit',
	                                         'menutop'        => 0,
	                                         'from'           => $this->Mfrom->loadFrom(),
	                                         'departamentos'  => $this->mdepartamento->LoadEstatus(),
	                                         'categoria'      => $this->Mcategoria->loadCategoriaFrom(),
	                                         'registro'       => $registro));
	    }
	    
	    public function delete()
	    {
	        $id = $this->input->post('delete_rows');
	        
	        if ($this->Mscript->daleteRegistro(array("idScript" => $id)) == true)
	            $this->session->set_flashdata('error_title', 'Registro Eliminado');

	    }
	    
	    public function save()
	    {
	         $data = array("titleScript"          => $this->input->post("title"),
	                       "subjectScript"        => $this->input->post("sub"),
	                       "fromScript"           => $this->input->post("from"),
	                       "tipoScript"           => $this->input->post("tipoM"),
	                       "bodyScript"           => $this->input->post("body"),
	                       "departamenoScript"    => $this->input->post("departamentos"),
	                       "categoriaScript"      => $this->input->post("categoria"));
	         
	            if ($this->input->post('id') == false){
				        
	            	    if ($this->Mscript->addRegistro($data) == true)
			                 $this->session->set_flashdata('error_title', 'Se a creado el registro');
			            else
			 	             $this->session->set_flashdata('error_title', 'Huvo un error');
			
		           }else{
			              if ($this->Mscript->updateRegistro($data,array("idScript" => $this->input->post('id'))) == true){
			                  $this->session->set_flashdata('error_title', 'Se Edit el Registro');
			              }else{
			 	              $this->session->set_flashdata('error_title', 'Huvo un error');
			              }        
			       }
		redirect('script');
	    }

}